<%--
  Created by IntelliJ IDEA.
  User: 恒少
  Date: 2021/3/19
  Time: 15:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>

    <link rel="stylesheet" href="layui/css/layui.css">
    <script src="layui/layui.js"></script>
</head>
<body>


<div class="layui-row">
    <div class="layui-col-xs6">
        <div class="grid-demo grid-demo-bg1">

            <table class="layui-hide" id="test" lay-filter="test"></table>

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
                </div>
            </script>

            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>




        </div>
    </div>
    <div class="layui-col-xs6">
        <div class="grid-demo">

            <table class="layui-hide" id="test1" lay-filter="test1"></table>
            <script type="text/html" id="toolbarDemo1">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
                </div>
            </script>

            <script type="text/html" id="barDemo1">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>


        </div>
    </div>
</div>


<script>
    layui.use(['table','jquery'], function () {
        var table = layui.table;
        var $ =layui.jquery;

        $(function () {
            aaa();
        })

        var arr = [];
        table.render({
            elem: '#test'
            , url: '${pageContext.request.contextPath}/book/query'
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , title: '用户数据表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'bookid', title: '书ID', fixed: 'left', unresize: true, sort: true}
                , {field: 'bookname', title: '书名'}
                , {field: 'author', title: '作者', sort: true}
                , {field: 'count', title: '库存'}
                , {field: 'type', title: '类别'}
                , {field: 'press', title: '出版社', sort: true}
                , {field: 'inprice', title: '进价', sort: true}
            ]]
            , page: false
        });

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    $.each(data,function (i,value) {
                        arr.push(value);
                    })
                    aaa();
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                layer.prompt({
                    formType: 2
                    , value: data.email
                }, function (value, index) {
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });

        function aaa(){
            table.render({
                elem: '#test1'
                , data: arr
                , toolbar: '#toolbarDemo1' //开启头部工具栏，并为其绑定左侧模板
                , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    , layEvent: 'LAYTABLE_TIPS'
                    , icon: 'layui-icon-tips'
                }]
                , title: '用户数据表'
                , cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'bookid', title: '书ID', fixed: 'left', unresize: true, sort: true}
                    , {field: 'bookname', title: '书名'}
                    , {field: 'author', title: '作者', sort: true}
                    , {field: 'count', title: '库存'}
                    , {field: 'plancount', title: '订购数量', edit: 'text',event:'edit'}
                    , {field: 'type', title: '类别'}
                    , {field: 'press', title: '出版社', sort: true}
                    , {field: 'inprice', title: '进价', sort: true}
                ]]
                , page: false
            });


        }

        //头工具栏事件
        table.on('toolbar(test1)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
            }
            ;


            //监听行工具事件
            table.on('tool(test1)', function (obj) {
                var data = obj.data;
                //console.log(obj)
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        obj.del();
                        layer.close(index);
                    });
                } else if (obj.event === 'edit') {
                    layer.prompt({
                        formType: 2
                        , value: data.email
                    }, function (value, index) {
                        obj.update({
                            email: value
                        });
                        layer.close(index);
                    });
                }
            });
        });
    });


</script>


</body>
</html>
